package com.markerhub.service.impl;

import com.markerhub.entity.TeachingStudentInClass;
import com.markerhub.mapper.ManagerStudentMapper;
import com.markerhub.mapper.TeachingStudentInClassMapper;
import com.markerhub.service.TeachingStudentInClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class TeachingStudentInClassServiceImpl extends ServiceImpl<TeachingStudentInClassMapper, TeachingStudentInClass> implements TeachingStudentInClassService {
    @Autowired
    ManagerStudentMapper studentMapper;
    @Autowired
    TeachingStudentInClassMapper studentInClassMapper;

    @Override
    public List<Integer> getStudentIdByClassId(int classId) {
        // 通过班级ID查询学生ID列表
        List<Integer> studentIds = studentInClassMapper.getStudentIdsByClassId(classId);

        // 通过学生ID列表查询学生姓名
        return studentIds;
    }
    @Override
    @Transactional
    public void updateClassStudents(Integer classId, List<Integer> studentIds) {
        // 删除当前班级已有的学员数据
        studentMapper.deleteByClassId(classId);
        // 批量添加新学员数据
        if (studentIds != null && !studentIds.isEmpty()) {
            List<TeachingStudentInClass> newStudents = studentIds.stream()
                    .map(studentId -> new TeachingStudentInClass(classId, studentId))
                    .collect(Collectors.toList());
            studentMapper.saveAll(newStudents);
        }
    }
}
